/**
 * 菜单数据表格
 */
Ext.namespace("Ext.ux.grid");

Ext.ux.grid.MenuGrid = Ext.extend(Ext.ux.grid.CrudGrid, {

	//是否启用行双击事件的事件处理函数，取值为true，为grid添加默认的行双击事件处理函数，取值为false不添加
	enabledDefaultRowDblclickEvent: false,
	
	initComponent: function(){
		
		// 查询表格表结构
		var gridStructure = [{  
			name: 'menuId',
			hidden: true
		},{
			name: 'menuCode',  
			header: "菜单编号",
			width: 80
		},{
			name: 'menuName',  
			header: "菜单名称",
			width: 100
		},{
			name: 'funcName',  
			header: "关联功能名称",
			width: 100
		},{
			name: 'menuLevel',  
			header: "菜单层次",
			width: 50
		},{
			name: 'sortNo',  
			header: "显示顺序",
			width: 50
		},{
			type: 'boolean',
			name: 'isLeaf',  
			header: "是否叶子节点",
			sortable: true,
			width: 50
		},{
			type: 'boolean',
			name: 'isActive',
			sortable: true,
			header: "是否启用",
			width: 50
		}];
		
		
		this.structure = gridStructure;
		this.defaultSortField = 'menuLevel';
		this.keyField = 'menuId';
		
		this.deleteMsg = '确认要删除选中的菜单吗？<br/><font color="red">注意</font>：删除菜单将同时删除与菜单相关的功能、快捷菜单关联信息。';
		this.deleteMsgBoxWidth = 450;

		Ext.ux.grid.MenuGrid.superclass.initComponent.call(this);
		
		//如果启用了行双击事件的处理函数
		if(this.enabledDefaultRowDblclickEvent){
			this.on('rowdblclick', function(menuGrid){
				var record = menuGrid.getSelectionModel().getSelected();
				if(record != null){
					var menuId = record.get('menuId');
					this.showMenuInfo(menuId);
				}
			});
		}
		
		
		this.on('updateRecord', function(menuId){
			this.showMenuInfo(menuId);
		});
		
		this.on('deleteRecord', function(grid, menuIds){
			this.deleteMenu(menuIds);
		});
	},
	
	//编辑菜单信息
	showMenuInfo: function(menuId){
		var win = new Ext.ux.org.acmenu.MenuDetailWindow({title:'修改菜单', menuId:menuId});
		win.showMenuInfo();
	},
	
	//执行删除菜单操作
	deleteMenu: function(menuIds) {
		Ext.Ajax.request({
			url: 'ac-menu!delete.action',
			success: function(){
				Ext.getCmp('menuTree').refreshMenu();
				this.store.reload();
			},
			failure: function(form, action) {
				Ext.Msg.alert('系统提示', '删除菜单失败！');
			},
			method: 'POST',
			params: {
				ids: menuIds
			},
			waitMsg: '正在删除数据，稍候...',
			scope: this
		});
	},
	
	//新增顶级菜单
	addTopMenu: function(){
		var win = new Ext.ux.org.acmenu.MenuDetailWindow({title:'新增顶级菜单'});
		win.show();
	},
	
	//新增子菜单
	addChildMenu: function(){
		var sel = this.getSelectionModel().getSelections();
		if(sel.length == 1){
			var parentMenuId = sel[0].get('menuId');
			var parentMenuName = sel[0].get('menuName');
			
			var win = new Ext.ux.org.acmenu.MenuDetailWindow({title:'新增子菜单', parentMenuId:parentMenuId, parentMenuName:parentMenuName});
			win.addChildMenu();
		}
		else{
			Ext.Msg.alert('系统提示', '请选择一条父菜单');
		}
	}
});